/*
 * Copyright (C) 2020 Agrui
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "IpcSimClient.h"
#include "IpcResult.h"
#include "IpcService.h"
#include "stdio.h"
#include "IpcSimService.pb.h"

IpcSimClient::IpcSimClient(int subId) : IpcClient(subId, IPC_SERVICE_SIM) {

}

IpcSimClient::~IpcSimClient() {

}

IPC_API_RESULT_T IpcSimClient::getSimState(void* userData, ipcServiceResultCallback callback) {
    auto cb = [callback, userData](int msgId, int result, void* data, int dataLen) {
        IpcGetSimStateResponse response;
        //int simState = SIM_STATE_UNKNOW;
        response.ParseFromArray(data, dataLen);
        int simState = response.state();
        if (result == IPC_RESULT_ERROR) {
            callback(IPC_API_RESULT_ERROR, NULL, 0, userData);
            return;
        }

        callback(IPC_API_RESULT_OK, &simState, sizeof(simState), userData);
    };

    IpcAsyncRequest* requestCbInfo = new IpcAsyncRequest(userData, callback, cb);
    sendAsyncRequest(IPC_SIM_REQUEST_MSG_ID::MSG_ID_GET_SIM_STATE, NULL, 0, requestCbInfo);
    return IPC_API_RESULT_OK;
}


